<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class YinTong {

    // 接口域名
    private $hostName = 'http://117.71.61.79:10022/';
    
    // 接口地址
    // private $apiUrl = "http://117.71.61.79:10022/etsInterface/gateway";

    private $apiUrl = "http://117.71.61.79:10720/etsInterface/gateway";

    private $xDataId;
    private $xDataParams;
    private $xDataToken;

	public function __construct($xDataId, $xDataParams, $xDataToken) {
        $this->xDataId = $xDataId;
        $this->xDataParams = $xDataParams;
        $this->xDataToken = $xDataToken;
    }

    // 部门同步接口
    
    public function get_dept_list(){
        $paramsData = array(
            'deptid' => null,
        );
        $funcName = 'sync/dept';
        $strBase64 = $this->handle_post_data($paramsData, $funcName);
        $result = $this->post($this->apiUrl, $strBase64);
        // echo '<pre>';var_export($result);exit;
        return $this->handle_result_data($result);
    }

    // 人员同步接口
    public function get_user_list(){
        $paramsData = array(
            'deptid' => null,
            'isall' => 1,
            'custver' => 0
        );
        $funcName = 'sync/custinfo';
        // $signatureStr = '{"appid":"1593564416","data":"{\"deptid\":null,\"isall\":1,\"custver\":0}","data_type":null,"function":"sync/custinfo","legal_person_code":"200701251873","sign":"77a0b75113010714e434a191108e39ea","sign_type":"MD5","version":"V1.0.0"}';
        // $strBase64 = base64_encode($signatureStr);
        $strBase64 = $this->handle_post_data($paramsData, $funcName);
        $result = $this->post($this->apiUrl, $strBase64);

        return $this->handle_result_data($result);

        // echo '<pre>';var_export($dataArr);exit;
    }
    
    // 人员照片同步接口
    public function get_user_photo_list($custcode){
        $paramsData = array(
            'custcode' => $custcode
        );
        $funcName = 'sync/custphoto';
        $strBase64 = $this->handle_post_data($paramsData, $funcName);
        $result = $this->post($this->apiUrl, $strBase64);

        return $this->handle_result_data($result);
    }

    // 请求数据格式处理
    private function handle_post_data($paramsData, $funcName) {
        $paramsDataStr = json_encode($paramsData, JSON_UNESCAPED_UNICODE);
        // 待签名字符串
        $str = 'key=' . $this->xDataToken . '&legal_person_code=' . $this->xDataParams . '&function=' . $funcName . '&appid=' . $this->xDataId . 
        '&version=V1.0.0&sign_type=MD5&data=' . $paramsDataStr;
        $strMd5 = md5($str);
        
        $signatureArr = array(
            'appid' => $this->xDataId,
            'data' => $paramsDataStr,
            'data_type' => null,
            'function' => $funcName,
            'legal_person_code' => $this->xDataParams,
            'sign' => $strMd5,
            'sign_type' => 'MD5',
            'version' => 'V1.0.0',
        );
        $signatureJson = json_encode($signatureArr, JSON_UNESCAPED_SLASHES);
        // 双反斜杆转义符改成单反斜杠
        $signatureJson = str_replace('\\\\', '\\', $signatureJson);
        return base64_encode($signatureJson);
    }


    // 返回接口格式处理
    private function handle_result_data($result) {
        $dataArr = json_decode(base64_decode($result), true);
        if($dataArr == NULL) {
            return false;
        }
        return $dataArr;
    }


    // 提交数据到服务器
    private function post($url, $data){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // 发送json类型的header
        $header = array("Content-Type: application/json");
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        $tmpInfo = curl_exec($ch);

        if(curl_errno($ch)) return curl_error($ch);
        curl_close($ch);
        return $tmpInfo;
    }
}